/**
 * @link https://leetcode.cn/problems/summary-ranges/?envType=study-plan-v2&envId=top-interview-150
 */
function summaryRanges(nums: number[]): string[] {
  let ans = [];
  let start = 0;
  let end = 0;
  for (let i = 0; i < nums.length; i++) {
    if (nums[i + 1] === nums[i] + 1) {
      end += 1;
    } else {
      if (start === end) ans.push(nums[start] + "");
      else ans.push(nums[start] + "->" + nums[end]);
      start = i + 1;
      end = i + 1;
    }
  }
  console.log(ans);
  return ans;
}

/**
 * @link https://leetcode.cn/problems/merge-intervals/?envType=study-plan-v2&envId=top-interview-150
 */
function merge(intervals: number[][]): number[][] {
  let ans: number[][] = [];
  intervals.sort((a, b) => a[0] - b[0]);
  let tmp: number[] = [];
  for (let i = 0; i < intervals.length; i++) {
    if (tmp.length === 0) tmp.push(intervals[i][0], intervals[i][1]);
    if (i + 1 < intervals.length && tmp[1] >= intervals[i + 1][0]) {
      tmp[1] = Math.max(tmp[1], intervals[i + 1][1]);
      continue;
    } else {
      if (intervals[i][1] > tmp[1]) {
        tmp[1] = intervals[i][1];
      }
      ans.push(tmp);
      tmp = [];
    }
  }
  return ans;
}

/**
 * @link https://leetcode.cn/problems/insert-interval/?envType=study-plan-v2&envId=top-interview-150
 */
function insert(intervals: number[][], newInterval: number[]): number[][] {
  let ans: number[][] = [];
  intervals.push(newInterval);
  let tmp = merge(intervals);
  console.log(tmp);
  return ans;
}

/**
 * @link https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons/?envType=study-plan-v2&envId=top-interview-150
 */
function findMinArrowShots(points: number[][]): number {
  let ans = 0;
  return ans;
}

export default { summaryRanges, merge, insert, findMinArrowShots };
